<template>
  <!-- 放置一个弹层 用来编辑新增节点 -->
  <el-dialog
    :title="isEdit ? '编辑权限' : '新增权限'"
    :visible.sync="showDialog"
    @close="btnCancel"
  >
    <!-- 表单 -->
    <el-form ref="perForm" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="formData.name" style="width: 90%" />
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="formData.code" style="width: 90%" />
      </el-form-item>
      <el-form-item label="权限描述" prop="description">
        <el-input v-model="formData.description" style="width: 90%" />
      </el-form-item>
      <el-form-item label="开启" prop="enVisible">
        <el-switch v-model="formData.enVisible" active-value="1" inactive-value="0" />
      </el-form-item>
    </el-form>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" type="primary" @click="btnConfirm">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      permissionList: [], // 权限管理列表数据
      formData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型吖
      },
      rules: {
        name: [
          { required: true, message: '权限名称不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '权限标识不能为空', trigger: 'blur' }
        ]
      },
      showDialog: false,
      isEdit: false
    }
  },
  methods: {
    // 确定按钮
    btnConfirm() {
      // eslint-disable-next-line
      this.$refs.perForm.validate(async (valid) => {
        if (valid) {
          if (!this.isEdit) {
            this.$emit('addPermissionEv', { ...this.formData })
          } else {
            this.$emit('editPermissionEv', { ...this.formData })
          }
          this.showDialog = false
        }
      })
    },

    // 取消按钮
    btnCancel() {
      this.$refs.perForm.resetFields()
      // 让 formData 回到data里的初始 formData 的值，相当于一次性把 formData 对象重置为初始 formData
      this.formData = this.$options.data().formData
      this.showDialog = false
    }
  }
}
</script>

<style></style>
